package yifeng.program;

public class Query {

	
	/**
	 * 二分查找
	 * @param array
	 */
	public static int binaryQuery(int[] array, int targetNumber, 
			int start, int end){
		
		int index = (end - start)/2 + start;
		if(array[index] == targetNumber){
			return index;
		}
		else if(array[index] < targetNumber){
			if(index + 1 > end)
				return -1;
			else return binaryQuery(array, targetNumber, index + 1, end);
		}
		else if(array[index] > targetNumber){
			if(index - 1 < start)
				return -1;
			else return binaryQuery(array, targetNumber, start, index - 1);
		}
		
		
		return -1;
	}
	
	
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] a = {1, 3, 5, 7, 9, 10};
		
		System.out.println(binaryQuery(a, 10, 0, a.length - 1));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
